package com.example.musicplayer.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.musicplayer.R;
import com.example.musicplayer.adapter.MusicListTestAdapter;
import com.example.musicplayer.entity.MusicListTest;

import java.util.ArrayList;
import java.util.List;

public class HomeFragment extends Fragment {
    private List<MusicListTest> mMusicListTests;
    public View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
    }

    private void initView() {
        initMusicList();
        initRecyclerView(R.id.recyclerview_1, LinearLayoutManager.HORIZONTAL);
        initRecyclerView(R.id.recyclerview_2, LinearLayoutManager.VERTICAL);
        initRecyclerView(R.id.recyclerview_3, LinearLayoutManager.HORIZONTAL);
        initRecyclerView(R.id.recyclerview_4, LinearLayoutManager.VERTICAL);
        initRecyclerView(R.id.recyclerview_5, LinearLayoutManager.HORIZONTAL);
    }

    /**
     * 初始化RecyclerView
     */
    private void initRecyclerView(@IdRes int resId, @RecyclerView.Orientation int orientation) {
        MusicListTestAdapter adapter;
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this.getContext());

        if (orientation == LinearLayoutManager.VERTICAL) {
            linearLayoutManager.setOrientation(orientation);
            adapter = new MusicListTestAdapter(mMusicListTests, R.layout.recyclerview_music_list_item_vertical);
        } else {
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            adapter = new MusicListTestAdapter(mMusicListTests, R.layout.recyclerview_music_list_item_horizontal);
        }

        RecyclerView recyclerView = view.findViewById(resId);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(adapter);

    }

    /**
     * 初始化歌单列表
     */
    private void initMusicList() {
        mMusicListTests = new ArrayList<>();
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
        mMusicListTests.add(new MusicListTest(R.drawable.album, "221万", "热门动漫主题曲合集"));
    }
}
